package com.qf.java2105.huangchuang.hotel.service.impl;

import com.qf.java2105.huangchuang.hotel.dao.IFoodDao;
import com.qf.java2105.huangchuang.hotel.entity.PageBean;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.pojo.Food;
import com.qf.java2105.huangchuang.hotel.service.IFrontService;

import java.util.List;

/**
 * 前台业务层实现
 * @author hcvg
 * @version v1.0
 * @date 2021/09/15
 **/
public class FrontServiceImpl implements IFrontService {

    IFoodDao foodDao = (IFoodDao) BeanFactory.getBean("foodDao");
    @Override
    public ResultVO<PageBean<Food>> findByPageWithCondition(Integer currentPage, Integer pageSize, Integer typeId, String foodName) {
        try {
            PageBean pageBean = new PageBean();
            // 设置当前页，setCurrentPage中校验当前页合理
            pageBean.setCurrentPage(currentPage);
            // 设置每页菜品条数
            pageBean.setPageSize(pageSize);

            // 调取count方法获取数据总数量
            Long count = foodDao.countByCondition(typeId, foodName);

            // limit 0, 6
            Integer start = (pageBean.getCurrentPage() - 1) * pageBean.getPageSize();
            // 调取findByPage方法获得菜品集合数据
            List<Food> foodList = foodDao.findByPageWithCondition(start, pageBean.getPageSize(), typeId, foodName);

            // 设置总数量
            pageBean.setTotalCount(count);
            // 设置总分页数
            pageBean.setTotalPage();
            // 设置集合数据
            pageBean.setList(foodList);
            // 查询成功返回
            return ResultVO.ok("查询分页成功", pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error("查询分页失败");
        }
    }
}
